package jwlee.testproxy.cglib;

import java.lang.reflect.Method;

import jwlee.testproxy.cglibproxy.TimeBook;
import jwlee.testproxy.cglibproxy.TimeBookInterface;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestHelloWorld {
	private static Logger logger = Logger.getLogger(TestHelloWorld.class.getName());
	public static void main(String[] args) {
//		testCglib();
		test();
	}

	private static void testCglib() {
		TimeBookInterface tbi = (TimeBookInterface) Enhancer.create(
				TimeBook.class, new MethodInterceptor() {
					@Override
					public Object intercept(Object obj, Method method,
							Object args[], MethodProxy methodproxy)
							throws Throwable {
						logger.log(Level.INFO," ----审核开始....");
						
						Object result = methodproxy.invokeSuper(obj, args);
						
						logger.log(Level.INFO," ----审核结束....");
						return result;
					}});
		tbi.doAuditing("mzj");
	}

	private static void test() {
		// 通过ApplicationContext获取XML
		ApplicationContext actx = new ClassPathXmlApplicationContext("jwlee/testproxy/cglibproxy/config.xml");
		//ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");

		TimeBookInterface timeBookProxy = (TimeBookInterface) actx.getBean("logProxy");

		timeBookProxy.doAuditing("张三审核数据");
		timeBookProxy.updateAudit("李四修改数据");
		// FinanceInterface financeProxy = (FinanceInterface)actx.getBean("logProxy1");
		// financeProxy.doCheck("李四");
	}
}
